package gensheixue;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ForkJoinPool;

public class b棒棒糖 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        scan.nextLine();
        int [][]arr = new int[m][];
        List<Integer> list = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < m; i++) {
            int si = set.size();
            String s = scan.nextLine();
            String[] s1 = s.split(" ");
            arr[i] = new int[s1.length];
            for (int j = 0; j < s1.length; j++) {
                if(Integer.parseInt(s1[j])==0){
                    set.add(i);
                }
                arr[i][j] = Integer.parseInt(s1[j]);
            }
            if(si!=set.size()){
                list.add(i);
            }
        }
        boolean b = true;
        Set<Integer> jie = new HashSet<>();
        for(Integer s:set){
            for (int i = 1; i < arr[s].length; i++) {
                jie.add(arr[s][i]);
            }
        }
        while(b){
            int size = jie.size();
            for (int i = 0; i < arr.length; i++) {
                if(list.contains(i)){
                    continue;
                }
                for (int j = 1; j < arr[i].length; j++) {
                    if(jie.contains(arr[i][j])){
                        for (int k = 1; k < arr[i].length; k++) {
                            jie.add(arr[i][k]);
                        }
                        list.add(i);
                        break;
                    }
                }
            }
            if(size==jie.size()){
                b = false;
            }
        }
        System.out.println(jie.size());


    }

    public int gensheixue(int N, int M) {
        if (N >= M) {
            return 1;
        }
        int max = 0;
        for (int i = 1; i < N; i++) {
//            int count = (1)
        }
        return 0;
    }
    public String [] a(String[] strs){
        String [] strNew = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            if(strs[i]==""){
                strNew[strs.length-1] = strs[i];
            }

//            for (int j = 0; j < i; j++) {
//                if(strNew[j]<strs[i]){
//
//                }
//            }
        }
        return null;
    }


}
class Node{
    int data;
    Node next;
    public Node(int data){
        this.data = data;
    }
}
class T{


    public static void main(String[] args) {
//        Scanner scan = new Scanner(System.in);
//        int n = scan.nextInt();
//        Node node = new Node(0);
//        for (int i = 0; i < n; i++) {
//            Node temp = node;
//            temp.next = new Node(scan.nextInt());
//            temp = temp.next;
//        }
//        Node temp = node;
//        while(temp!=null){
//            System.out.println(temp.data);
//            temp = temp.next;
//        }
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int order[] = new int[n+1];
        int time[] = new int[n + 1];
        for (int i = 1; i < n+1; i++) {
            order[i] = i;
            time[i] = scan.nextInt();
        }
        int m = scan.nextInt();
        int dp[] = new int[m+1];
//        for (int i = 1; i < n+1; i++) {
//            for (int j = m; j >order[i-1 ] ; j--) {
//                int i1 = dp[j - order[i - 1]] + time[i - 1];
//                dp[j] = Math.max(dp[j-day[i-1]] + value[i-1], dp[j]);
//            }
//        }
    }


}
